<?php
namespace Company\Controller;
use Think\Controller;
class BaseController extends Controller {
    public function _initialize(){
		if(null==session('company')){
            $this->redirect('Reg/login');
        }
		$company_status=session('company.status');
		$childs=session('company.childs');
		
		if($company_status<>1){
			$this->error('账号正在审核中，请等待通过后再登录。。。');
		}
		if(!empty($childs)){
			$company_cilids_status=$childs['status'];
			if($company_cilids_status<>1){
				$this->error('账号正在审核中，请等待通过后再登录。。。');
			}
			$this->assign('childs_company',$childs);
			$g=session('company.childs');
		}else{
			$g=session('company');
		}
		
        $g=session('company');
		
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',$g['db']);
		//数据库连接基础配置-end
		
        $this->rootCheck();
		//////权限检查
		// $this->authCheck();
    }
	
	protected function authCheck(){
       $department_id=session('company.department_id');
	   
       $m = M('department',C('DB_PREFIX_T'),C('db_config'));
	   $r = M('root',C('DB_PREFIX_T'),C('db_config'));
	  
       $root_group_id=$m->where(array('id'=>$department_id))->getField('root_group_id');
	   
       $a_id=$r->where(array('controller'=>CONTROLLER_NAME))->getField('action,id',true);
       $group_id=explode(',',$root_group_id);
       $no_arr=array(	//额外的都具备的权限
           'Index'	=>	array('index','welcome'),
           'Role'	=>	array('getDepartment','getRole','getEditRole'),
       );
       foreach($a_id as $k=>$v){
           $key=explode(',',$k);
		   foreach($key as $ks=>$vs){
			   if($vs==ACTION_NAME){
				   $root_id=$v;
				   $is_check=$r->where(array('id'=>$v))->getField('is_check');
				   if($is_check){//1：验证权 0：不验证权限（默认）
					    if(!in_array($root_id,$group_id)){
						   if(!in_array(ACTION_NAME,$no_arr[CONTROLLER_NAME])){
							   alert(0,'没有权限');
						   }
						}
				   }
			   }
		   }
       }
   }
   
    protected function rootCheck(){

		$base_config=M('Config',C('DB_PREFIX_T'),C('db_config'));//加载配置信息
		$b=M('Book',C('DB_PREFIX_T'),C('db_config'));
		$list=$base_config->getField('en_name,value');
		C('base_config',$list);
		
		//商家编码--begin
		// $importExcelPushorderTempName=C('base_config.importExcelPushorderTempName');
		// if(!empty($importExcelPushorderTempName)){
			// $b_count=$b->count();
			// if($b_count>1){
				// $return['status']=0;
				// $return['message']="多账册请传入账册编码";
				// $this->ajaxReturn($return);
			// }
			// $b_info=$b->find();
			// $merchant_code=$b_info['merchant_code'];//商家编码
			// C('base_config.merchant_code',$merchant_code);
		// }
		//商家编码--end
		
       
    }
}